package com.ysstech.system.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.entity.Dicvalue;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.StringUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Slf4j
@Api(tags = "redis缓存管理api")
@RequestMapping(value = "/redis")
public class RedisCacheController extends BaseController {


    @YssPermissions(value = {"system:redis"})
    @ApiOperation(value = "跳转Redis缓存管理页面")
    @YssLog(module = "Redis缓存管理", remark = "跳转Redis缓存管理页面")
    @RequestMapping("/page")
    public String page() throws Exception {
        return "system/redis";
    }

    /* 角色列表信息查询
     * 查询当前登录人所在机构和所有下级机构用户
     *
     * @return
     */
    @YssPermissions(value = {"system:redis"})
    @ApiOperation(value = "获取Redis所有缓存信息")
    @YssLog(module = "Redis缓存管理", remark = "获取Redis所有缓存信息")
    @RequestMapping(value = "/listInfo")
    @ResponseBody
    public RestResult listInfo(String selCode) throws Exception {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> mapreids = redisCacheManager.getAllCache();
        List<Dicvalue> list = new ArrayList<>();
        for (Map.Entry<String, Object> entry : mapreids.entrySet()) {
            String key = entry.getKey();
            if (StringUtils.isBlank(key)) {
                continue;
            }
            if (StringUtils.isNotBlank(selCode) && !StringUtil.oneOf(key, selCode)) {
                continue;
            }
            Dicvalue dicvalue = new Dicvalue();
            dicvalue.setCode(entry.getKey());
            dicvalue.setValue(String.valueOf(entry.getValue()));
            list.add(dicvalue);
        }
        map.put("rows", list);
        map.put("total", list.size());
        return ResultGenerator.getSuccessResult("获取Redis所有缓存信息成功", map);
    }

    @YssPermissions(value = {"system:redis:del"})
    @ApiOperation(value = "删除Redis缓存信息")
    @YssLog(module = "redis缓存管理", remark = "删除Redis缓存信息")
    @RequestMapping(value = "/delRedis")
    @ResponseBody
    public RestResult delRedis(String codes) throws Exception {
        if (StringUtils.isBlank(codes)) {
            throw new BusinessException("获取删除Redis缓存信息失败，请稍后重试！");
        }
        List<String> list = JSONArray.parseArray(codes, String.class);
        for (String key : list) {
            redisCacheManager.removeCache(key);
        }
        return ResultGenerator.getSuccessResult("删除Redis缓存信息成功");
    }
}
